package edu.neusoft.yuejian_09.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import edu.neusoft.yuejian_09.R;
import edu.neusoft.yuejian_09.base.Constant;
import edu.neusoft.yuejian_09.base.RetrofitListener;
import edu.neusoft.yuejian_09.bean.Collection;
import edu.neusoft.yuejian_09.bean.Master;
import edu.neusoft.yuejian_09.fragment.MasterClassFragment;
import edu.neusoft.yuejian_09.fragment.MasterQuesFragment;
import edu.neusoft.yuejian_09.model.CollectModel;
import edu.neusoft.yuejian_09.model.MasterModel;
import it.sephiroth.android.library.picasso.Picasso;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static edu.neusoft.yuejian_09.base.Constant.PHOTO_URL;

public class MasInfoActivity extends FragmentActivity implements View.OnClickListener,RetrofitListener {

    TextView m_name,m_info,m_address,m_class,m_question;
    ImageView img_return,img_collect,m_pic;


    int master_id;
    String master_name;
    Object master_info;
    String master_address;
    String master_pic;

    int user_id;
    private Collection collectResult;
    private String coresult="";

    private LinearLayout mTabClass;
    private LinearLayout mTabQuestion;

    private Fragment mTab01;
    private Fragment mTab02;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        setContentView(R.layout.activity_mas_info);
        initViews();
        initEvents();
        initData();
        setSelect(0);
        //saveMaster();
    }

    public void initViews(){
        m_name=(TextView)findViewById(R.id.master_name);
        m_address=(TextView)findViewById(R.id.address);
        m_info=(TextView)findViewById(R.id.info);
        img_return=(ImageView)findViewById(R.id.img_return);
        img_collect=(ImageView)findViewById(R.id.img_collect);
        m_pic=(ImageView)findViewById(R.id.master_pic);


        /*************fragment***************/
        mTabClass= (LinearLayout) findViewById(R.id.linear_class);
        mTabQuestion= (LinearLayout) findViewById(R.id.linear_question);
        m_class=(TextView)findViewById(R.id.classes);
        m_question=(TextView)findViewById(R.id.question);
        /*************fragment***************/

    }

    public void initEvents(){
        img_return.setOnClickListener(new View.OnClickListener() {//返回
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        img_collect.setOnClickListener(new View.OnClickListener() {//收藏
            @Override
            public void onClick(View v) {
                collectMaster();
            }
        });

        /*************fragment***************/
        mTabClass.setOnClickListener(this);
        mTabQuestion.setOnClickListener(this);
        /*************fragment***************/
    }

    public void initData(){

        getMasterInfo();
        getUserId();
        checkCollect();

    }

    //获取大师信息数据
    public void getMasterInfo(){

        Bundle bundle = getIntent().getExtras();
        master_id = bundle.getInt("master_id");
        MasterModel masterModel = new MasterModel();
        masterModel.getMasterById(master_id,this);

    }

    //获取个人用户信息
    public void getUserId(){
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        user_id = sharedPreferences.getInt("user_id", 0);
    }

    //收藏比赛
    private void collectMaster( ){
        CollectModel collectModel = new CollectModel();
        collectModel.collectMaster(user_id,master_id,this);
    }

    //检查收藏状态
    private void checkCollect(){
        CollectModel collectModel = new CollectModel();
        collectModel.isCollected(user_id,master_id,3,this);

    }

    private void refresh(){
        checkCollect();
    }

    @Override
    public void onSuccess(Object o, int flag) {
        switch (flag){
            case Constant.MASTERBYID:
                Master master=(Master)o;
                master_name = master.getMaster_name();
                master_pic =PHOTO_URL+master.getMaster_bgpic();
                master_address = master.getClub_name();
                master_info=master.getMaster_info();
                m_name.setText(master_name);
                m_info.setText(master_info+" ");
                m_address.setText(master_address);
                if (master_pic != "") {
                    Picasso.with(MasInfoActivity.this)
                            .load(master_pic)
                            .into(m_pic);
                    m_pic.setScaleType(ImageView.ScaleType.FIT_XY);
                } else {
                    m_pic.setImageResource(R.drawable.no_pic);
                }
                break;
            case Constant.COLLECTMASTER:
                Collection collection=(Collection)o;
                if ("yes".equals(coresult)){
                    Toast.makeText(MasInfoActivity.this,"取消收藏成功！",Toast.LENGTH_SHORT).show();
                    img_collect.setImageResource(R.drawable.ic_collect);
                }else{
                    Toast.makeText(MasInfoActivity.this,"收藏成功！",Toast.LENGTH_SHORT).show();
                    img_collect.setImageResource(R.drawable.ic_collect1);
                }
                refresh();
                break;
            case Constant.COLLECTSTATE:
                Collection collectstate=(Collection)o;
                String result = collectstate.getCollected();
                System.out.printf(result);
                if ("0".equals(result)){
                    img_collect.setImageResource(R.drawable.ic_collect);
                    coresult = "no";
                }else {
                    coresult = "yes";
                    img_collect.setImageResource(R.drawable.ic_collect1);
                }
                break;
        }
    }
    @Override
    public void onFail(){ Toast.makeText(MasInfoActivity.this, "网络发生错误", Toast.LENGTH_SHORT).show();}


    //大师课堂以及大师问答Fragment嵌入

    private void setSelect(int i)//一个是点击改变图片的背景色
    {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        switch (i)
        {
            case 0:
                if (mTab01 == null)
                {
                    mTab01 = new MasterClassFragment();
                    transaction.add(R.id.fragment_master, mTab01);
                } else
                {
                    transaction.show(mTab01);
                }
                m_class.setTextColor(getResources().getColor(R.color.wordGreen));
                break;
            case 1:
                if (mTab02 == null)
                {
                    mTab02 = new MasterQuesFragment();transaction.add(R.id.fragment_master, mTab02);
                } else
                {
                    transaction.show(mTab02);

                }
                m_question.setTextColor(getResources().getColor(R.color.wordGreen));
                break;

            default:
                break;
        }

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction)
    {
        if (mTab01 != null)
        {
            transaction.hide(mTab01);
        }
        if (mTab02 != null)
        {
            transaction.hide(mTab02);
        }
    }

    @Override
    public void onClick(View v)
    {
        resetImgs();
        switch (v.getId())
        {
            case R.id.linear_class:
                setSelect(0);
                break;
            case R.id.linear_question:
                setSelect(1);
                break;
            default:
                break;
        }
    }
    private void resetImgs()
    {
        m_class.setTextColor(getResources().getColor(R.color.wordYellow));
        m_question.setTextColor(getResources().getColor(R.color.wordYellow));
    }




}
